package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.EventBeanLocal;

/**
 *
 * @author pwitek
 */
public class EventController extends HttpServlet {
    @EJB
    private EventBeanLocal bean;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            if (action != null) {
                if (action.equals("get"))
                    getEvents(request, out);
                else if (action.equals("insert"))
                    insertEvent(request, out);
                else if (action.equals("update"))
                    updateEvent(request, out);
                else if (action.equals("delete"))
                    deleteEvent(request, out);
                else
                    out.println("Unknown action");
            } else
                out.println("Null action");
        } catch (Exception e) {
            out.println(e.getMessage());
        } finally {
            out.close();
        }
    }

    private void getEvents(HttpServletRequest request, PrintWriter out) {
        String param_sw = request.getParameter("sw");
        String param_ne = request.getParameter("ne");
        String param_year = request.getParameter("year");
        String param_day = request.getParameter("day");
        if (param_ne != null && param_sw != null && param_year != null && param_day != null) {
            String[] sw_coords = param_sw.split(",");
            String[] ne_coords = param_ne.split(",");
            if (sw_coords.length == 2 && ne_coords.length == 2) {
                Double sw_lat = Double.parseDouble(sw_coords[0]);
                Double sw_lng = Double.parseDouble(sw_coords[1]);
                Double ne_lat = Double.parseDouble(ne_coords[0]);
                Double ne_lng = Double.parseDouble(ne_coords[1]);
                Integer year = Integer.parseInt(param_year);
                Integer day = Integer.parseInt(param_day);
                out.write(bean.getEvents(sw_lat, sw_lng, ne_lat, ne_lng, year, day));
            } else {
                out.write("Wrong parameters: "+param_ne+" "+param_sw);
            }
        } else {
            out.write("Null parameters");
        }
    }

    private void insertEvent(HttpServletRequest request, PrintWriter out) {
        String param_latLng = request.getParameter("latLng");
        String param_year = request.getParameter("year");
        String param_day = request.getParameter("day");
        if (param_latLng != null && param_year != null && param_day != null) {
            String[] coords = param_latLng.split(",");
            if (coords.length == 2) {
                Double lat = Double.parseDouble(coords[0]);
                Double lng = Double.parseDouble(coords[1]);
                Integer year = Integer.parseInt(param_year);
                Integer day = Integer.parseInt(param_day);
                bean.insertEvent(lat, lng, year, day);
            } else {
                out.write("Wrong parameters: "+param_latLng);
            }
        } else {
            out.write("Null parameters");
        }
    }

    private void updateEvent(HttpServletRequest request, PrintWriter out) {
        String param_id = request.getParameter("id");
        String param_title = request.getParameter("title");
        String param_description = request.getParameter("description");
        String param_type = request.getParameter("type");
        String param_latLng = request.getParameter("latLng");
        String param_year = request.getParameter("year");
        String param_day = request.getParameter("day");
        if (param_id != null && param_title != null && param_description != null && param_type != null && param_latLng != null) {
            String[] coords = param_latLng.split(",");
            if (coords.length == 2) {
                Long id = Long.parseLong(param_id);
                Double lat = Double.parseDouble(coords[0]);
                Double lng = Double.parseDouble(coords[1]);
                Integer year = Integer.parseInt(param_year);
                Integer day = Integer.parseInt(param_day);
                bean.updateEvent(id, param_title, param_description, param_type, lat, lng, year, day);
            } else {
                out.write("Wrong parameters: "+param_latLng);
            }
        } else {
            out.write("Null parameters");
        }
    }

    private void deleteEvent(HttpServletRequest request, PrintWriter out) {
        String param_id = request.getParameter("id");
        if (param_id != null) {
            Long id = Long.parseLong(param_id);
            bean.deleteEvent(id);
            out.write("Event with id = "+id+" removed.");
        } else {
            out.write("Null parameters");
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}
